package com.tedu.api.ee;

import org.junit.Test;

import java.io.*;

public class HomeWork01 {

    /*作业：五个异常*/
    @Test
    public void testYC01(){
        FileInputStream fis=null;
        try {
            fis=new FileInputStream("aoe.txt");
            fis.read();
            fis.close();
        } catch (IOException e) {//IO流异常
            e.printStackTrace();
        } finally {
            if(fis!=null)
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    }
    @Test
    public void testYC02() {
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        try {
            fos = new FileOutputStream("ab3"+ File.separator+"BOS标准流写法.txt");
            bos = new BufferedOutputStream(fos);
            bos.write("标准流写法".getBytes());
            bos.flush();
        } catch (FileNotFoundException e) {//文件不存在异常：路径写错，文件不存在
            e.printStackTrace();
            System.out.println("");
        } catch (IOException e) { //IO流异常
            e.printStackTrace();
            System.out.println("");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("");
        } finally {

            if (bos != null)
                try {bos.close();} catch (IOException e) {e.printStackTrace();}
        }
    }
    @Test
    public void testYC03(){
        PrintWriter pw=null;
        try {
            pw=new PrintWriter("pw.txt","UTF8");
        } catch (FileNotFoundException e) {//文件找不到
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {//不支持抛出
            e.printStackTrace();
        } finally {
            if(pw!=null)
                pw.close();
        }
        pw.print("邪恶栀子花计划~");
        pw.println("这豆角没熟！");
        pw.close();
    }


    /*YC04*/
    public static void main(String[] args) {
        String s = null; //字符串s赋值为null
        try {
            int length = s.length(); // 尝试获取空字符串的长度
            System.out.println("字符串的长度为: " + length);
        } catch (NullPointerException e) {//数值转换异常
            System.out.println("发生异常: " + e.getMessage());
        }
    }




}
